import requests
from bs4 import BeautifulSoup as bs
import json
from sql_db import MySqlHelper


def read_db_book(url, book_dict_all=None):
    if book_dict_all is None:
        book_dict_all = dict()
    proxies = {
        "http": "113.195.97.110",
    }
    response = requests.get(url, proxies=proxies)
    if response.status_code == 200:
        bs_obj = bs(response.text, "html.parser")
        li_list = bs_obj.find(id='content').find_all('li')
        num = 0
        for book in li_list:
            num += 1
            dict_1 = dict()
            box = book.find('div')
            link_img = book.find('a')
            detail_url = link_img.attrs['href']
            link_img = link_img.find('img')

            if 'src' in link_img.attrs:
                link_img_url = link_img.attrs['src']
            else:
                link_img_url = ""

            name = box.find('h2').text.strip()
            # grade = box.find('p')
            # grade = grade.find_all('span')
            grade = box.find(
                'span', attrs={'class': 'font-small'}).get_text().strip()  # 评分
            print(grade)
            publish = box.select_one('p.color-gray').text.strip()
            print(publish)
            div = bs_obj.find(id='content')
            c = div.find('div', class_='article')
            c1 = div.find('div', class_='aside')
            if c1:
                cls = c1.find('h2').text.strip('  · · · · · · ')
                digest = box.select('p')[2].text.strip()
            else:
                cls = c.find('h2').text.strip('  · · · · · · ')
                digest = box.select_one('p.detail').text.strip()

            dict_1['name'] = name
            dict_1['detail_url'] = detail_url
            dict_1['link_img_url'] = link_img_url
            if grade:
                dict_1['grade'] = grade
            else:
                dict_1['grade'] = '暂无评分'
            dict_1['publish'] = publish
            dict_1['digest'] = digest
            dict_1['cls'] = cls
            book_dict_all[num] = dict_1
            # print(book_dict_all)
            print('保存成功')

    else:
        print('你抓取的出问题了')
    return book_dict_all


if __name__ == "__main__":
    url = 'https://book.douban.com/latest?icn=index-latestbook-all'
    book_dict_all = read_db_book(url)
    # print(book_dict_all)
    # for i in book_dict_all.values():
    #     for s in i.values():
    #         print(s)
    # print('2222222222222222222222222222222')
    # print(book_dict_all)
    mysql = MySqlHelper()
    sql = "INSERT INTO db_book(name, detail_url, link_img_url, grade, publish, digest, cls) VALUES(%s,%s,%s,%s,%s,%s,%s)"
    print('111111111111111111111111111')
    for i in book_dict_all.values():
        print(i)
        val = [value for value in i.values()]
        mysql.exec_many(sql, [val])

